package handler;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import message.Command;
import message.CommandType;

import java.util.EnumMap;

public class CommandHandler extends ChannelInboundHandlerAdapter
{
    private volatile EnumMap<CommandType, CommandProcessor> processors;

    public CommandHandler(EnumMap<CommandType, CommandProcessor> processors)
    {
        this.processors = processors;
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception
    {
        Command command = (Command) msg;
        processors.get(command.type()).process(command, ctx);
    }

    public void setProcessors(EnumMap<CommandType, CommandProcessor> processors)
    {
        this.processors = processors;
    }

    public static interface CommandProcessor
    {
        void process(Command command, ChannelHandlerContext ctx);
    }
}
